
u "$data_clean_failure/riskdata_analysis_2004.dta",clear
drop if pct_male==.
keep if year==2004


*--------------------------------------*
*          Global: outcoms             *
*--------------------------------------* 
** subprime
global Subprime "pct_subprime pct_subprime_conv"
global Subprime_type "pct_subprime_first pct_subprime_second pct_subprime_homequity pct_subprime_refi"

** approval decisions
global Approval "log_applications log_originations log_subprime"

** performance
global Performance "fail delqRatio delqRatio_mort netChargeOffRatio netChargeOffRatio_mort"
global Performance_type "delqRatio_fstmort netChargeOffRatio_fstmort delqRatio_secmort netChargeOffRatio_secmort"


*--------------------------------------*
*          			  PSM          	      		   *
*--------------------------------------* 
teffects psmatch (pct_subprime) (bank log_assets_04 numState_04 roa_04 avg_income pct_male pct_white pct_black $State), caliper(0.07)  osample(violate)
drop if violate==1
egen id_match = group(id)

preserve
keep if bank==1 
gen N=1
save "$data_clean_failure/bank_2004.dta", replace
restore

preserve 
keep if bank==0
save "$data_clean_failure/cu_2004.dta", replace
restore


teffects psmatch (pct_subprime) (bank log_assets_04 numState_04 roa_04 avg_income pct_male pct_white pct_black $State), caliper(0.07)  gen(num)
keep if bank==1
keep num1
ren num1 id_match
bysort id_match: gen N = _N
bysort id_match: keep if _n==1

merge 1:1 id_match using "$data_clean_failure/cu_2004.dta"
keep if _merge==3

append  using "$data_clean_failure/bank_2004.dta"
save "$data_clean_failure/cu_2004.dta"


// teffects overlap
keep id N
save "$data_clean_failure/matched_04.dta",replace


*--------------------------------------*
*          	     final match         	       *
*--------------------------------------* 
merge 1:m id using  "$data_clean_failure/riskdata_analysis_2004.dta"
keep if _merge==3
save  "$data_clean_failure/riskdata_analysis_2004_matched.dta", replace


